/**
 *	\file AKTTTAIPlayerTests.m
 *  
 *  \section desc_sec Description
 *  Test cases for AKTTTAIPlayer
 *  
 *  \if copyright
 *  	Copyright 2008 Ben Chen. All rights reserved.
 *  \endif
 *  
 *  \section hist_sec Change History:
 *  	-	2/25/08		BC		Created this file
 *  
 *  <hr>
 *  \addtogroup AKTTTTest
 *  @{
 */

#import <SenTestingKit/SenTestingKit.h>
#import "AKCommon.h"
#import "AKTTT.h"
#import "AKTTTAIPlayer.h"

#define BestMove @"bestMove"

@interface AKTTTAIPlayerTests : SenTestCase {
	AKTTTAIPlayer *player;
	AKTTTAIPlayer *opponent;
	
	AKPiece			cells[9];
	
	NSString		*bundlePath;
	NSBundle		*bundle;
}

@end

@implementation AKTTTAIPlayerTests

- (void) setUp {
	[super setUp];
	bundlePath	= [[NSString alloc] initWithString:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"/AKTTT.bundle"]];
	bundle		= [NSBundle bundleWithPath:bundlePath];

	Class playerClass = [bundle classNamed:@"AKTTTAIPlayer"];
	player		= [[playerClass alloc] initWithSide:0 numOfPlayers:2];
	opponent	= [[playerClass alloc] initWithSide:1 numOfPlayers:2];
	
	int i;
	for (i = 0; i < 9; ++i)
	{
		cells[i].side	= NSNotFound;
		cells[i].piece	= NSNotFound;
		cells[i].cell	= i;
	}
}

- (void) tearDown {
	[bundlePath		release];
	[bundle			release];
	
	[player release];

	[super tearDown];
}

- (void) testPerformMove {
	AKMove newMove;
	newMove.side = 0;
	newMove.piece = 0;
	newMove.dstCell = 0;
	newMove.srcCell = NSNotFound;
	
	[player backToInitialState];
	
	int i;
	for (i = 0; i < 9; ++i)
	{
		newMove.side = i % 2;
		newMove.dstCell = i;
		STAssertEquals(YES, [player performMove:newMove], @"performMove failed");
		STAssertEquals(NO, [player performMove:newMove], @"Next move cannot be placed at the same location");
	}
	
	STAssertEquals(NO, [player performMove:newMove], @"The chessboard should be full");
}

- (BOOL) moveIsLeagal:(AKMove)newMove isOver:(BOOL*)flag {
	BOOL retVal = YES;
	static unsigned count = 0;
	
	*flag = NO;
	
	if (count >= 9 || cells[newMove.dstCell].piece != NSNotFound || newMove.piece >= 9 || newMove.side > 1)
	{
		retVal = NO;
	}
	else
	{
		cells[newMove.dstCell].piece = newMove.piece;
		cells[newMove.dstCell].side = newMove.side;
		count++;
		
		if (count == 9)
			*flag = YES;
	}
	
	return retVal;
}
/*
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	if ([keyPath isEqualToString:BestMove])
	{
		NSLog(@"Hey");
		[(id)object removeObserver:self forKeyPath:BestMove];
		
		AKMove newBestMove = [(AKTTTAIPlayer*)object bestMove];
		
		BOOL isOver;
		STAssertEquals(YES, [self moveIsLeagal:newBestMove isOver:&isOver],	@"The new move is illegal.");
		
		STAssertEquals(YES, [player performMove:newBestMove],	@"Player cannot perform move.");
		STAssertEquals(YES, [opponent performMove:newBestMove],	@"Opponent cannot perform move.");
		
		if (!isOver)
		{
			AKTTTAIPlayer *nextPlayer = [object isEqualTo:player] ? opponent : player;
			[self continueWithPlayer:nextPlayer];
		}
	}
}

- (void)continueWithPlayer:(AKTTTAIPlayer *)aPlayer
{
	NSLog(@"Hello?");

	[(id)aPlayer addObserver:self forKeyPath:BestMove options:NSKeyValueObservingOptionNew context:NULL];
	STAssertEquals(YES, [aPlayer thinkNextMove], @"Player:%@ can't think", aPlayer);
}

- (void)testThinking
{
	[player		backToInitialState];
	[opponent	backToInitialState];
	
	[self continueWithPlayer:player];
}
 */

@end

/** @} */
